package com.example.springqrcraft.controller;

import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeGenWrapper;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 孤霜
 * @version 1.0
 */
@Controller
@ResponseBody
public class QrCraft {
    @GetMapping("/qr")
    public void qr(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse
                   ) {
        try {
            String url = httpServletRequest.getParameter("url");
            Map map = new HashMap<>();
            map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            map.put(EncodeHintType.CHARACTER_SET, "utf-8");
            map.put(EncodeHintType.MARGIN, 1);
            MultiFormatWriter writer = new MultiFormatWriter();
            BitMatrix bitMatrix = writer.encode(url, BarcodeFormat.QR_CODE, 300, 300, map);
            int width = bitMatrix.getWidth();
            int height = bitMatrix.getHeight();
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            for(int x=0;x<width;x++) {
                for(int y=0;y<height;y++) {
                    image.setRGB(x,y,bitMatrix.get(x,y)?0xFF000000:0xFFFFFFFF);
                }
            }
            ServletOutputStream out= httpServletResponse.getOutputStream();
            ImageIO.write(image,"png",out);
            out.flush();
            out.close();
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @PostMapping("/myqrcode/generateWithLogo")
    public void generateWithLogo(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse
    ) {
        try {
            String url = httpServletRequest.getParameter("url");
            Map map = new HashMap<>();
            map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            map.put(EncodeHintType.CHARACTER_SET, "utf-8");
            map.put(EncodeHintType.MARGIN, 1);
            MultiFormatWriter writer = new MultiFormatWriter();
            BitMatrix bitMatrix = writer.encode(url, BarcodeFormat.QR_CODE, 300, 300, map);
            int width = bitMatrix.getWidth();
            int height = bitMatrix.getHeight();
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            for(int x=0;x<width;x++) {
                for(int y=0;y<height;y++) {
                    image.setRGB(x,y,bitMatrix.get(x,y)?0xFF000000:0xFFFFFFFF);
                }
            }

            Part logoPart =httpServletRequest.getPart("logo");
            InputStream inputStream = logoPart.getInputStream();
            Image logoImage = ImageIO.read(inputStream);
            int logoWidth=logoImage.getWidth(null);
            int logoHeight=logoImage.getHeight(null);
            if(logoWidth>60){
                logoWidth=60;
            }
            if(logoHeight>60){
                logoHeight=60;
            }
            Image scaledLogo = logoImage.getScaledInstance(logoWidth, logoHeight, Image.SCALE_SMOOTH);
            Graphics2D graphics = image.createGraphics();
            int x= (300-logoWidth)/2;
            int y=(300-logoHeight)/2;
            graphics.drawImage(scaledLogo,x,y,null);
            Shape shape = new RoundRectangle2D.Float(x, y, logoWidth, logoHeight, 10, 10);
            graphics.setStroke(new BasicStroke(4f));
            graphics.draw(shape);
            graphics.dispose();
            ServletOutputStream out= httpServletResponse.getOutputStream();
            ImageIO.write(image,"png",httpServletResponse.getOutputStream());
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ServletException e) {
            e.printStackTrace();
        }

    }

    @PostMapping("/myqrcode/backgrop")
    public void backgrop(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse
    ) throws IOException, WriterException {
            // 默认属于全覆盖的背景模式，对应下图中左图
            String msg = "https://mvnrepository.com";
            String bg = "http://43.139.56.166:9001/yuanfang/imgs/鱼聪明AI绘画.jpg";
            boolean ans = QrCodeGenWrapper.of(msg)
                    .setBgImg(bg)
                    .setW(500)
                    .setBgOpacity(0.5f)
                    .asFile("/tmp/bqr1.png");

    }
}
